cd E:\17_HuaDong\teach\MyLecture\R\code
use DID,replace  //使用系统自带数据库
xtset idcode year, delta(1)  //设置面板
xtdescribe   //描述一下这个面板数据情况
gen age2= age^2
gen ttl_exp2=ttl_exp^2
gen tenure2=tenure^2
global xlist "grade age age2 ttl_exp ttl_exp2 tenure tenure2 not_smsa south race"
sum ln_w $xlist  //统计描述相关变量

gen time = (year >= 77) & !missing(year)  //政策执行时间为1977年
gen treated = (idcode >2000)&!missing(idcode) //政策执行地方为idcode大于2000的地方
gen did = time*treated  //这就是需要估计的DID，也就所交叉项
reg ln_w did time treated $xlist //这就是一个OLS回归，也可以用diff命令
xtreg ln_w did time treated $xlist i.year, fe //也可以这去做，会省略掉一个虚拟变量

tab year, gen(yrdum) //产生year dummy，即每一年一个dummy变量
     forval v=1/7{
gen treated`v'=yrdum`v'*treated
}                     //这个相当于产生了政策实行前的那些年份与处理虚拟变量的交互项
xtreg ln_w did treated*  i.year ,fe  //这个没有加控制变量
xtreg ln_w did treated* $xlist i.year ,fe //如果did依然显著，且treated*这些政策施行前年份交互项并不显著，那就好
